package org.example.demo2;

import org.example.demo2.Bank.BankingService;
import org.example.demo2.Restaurant.RestaurantService;
import org.example.demo2.Shape.ShapeService;
import org.example.demo2.Zoo.ZooManager;
import javax.swing.*;
import java.awt.*;

class IMSDashboard {
    // References to module services (would be initialized with actual implementations)
    private ZooManager zooManager;
    private BankingService bankingService;
    private RestaurantService restaurantService;
    private ShapeService shapeService;

    public IMSDashboard() {
        // Initialize the main frame
        JFrame frame = new JFrame("Integrated Management System");
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setSize(800, 600);
        frame.setLayout(new BorderLayout());

        // Create header panel
        JPanel headerPanel = new JPanel();
        headerPanel.setBackground(new Color(70, 130, 180));
        JLabel titleLabel = new JLabel("Integrated Management System Dashboard");
        titleLabel.setFont(new Font("Arial", Font.BOLD, 24));
        titleLabel.setForeground(Color.WHITE);
        headerPanel.add(titleLabel);
        frame.add(headerPanel, BorderLayout.NORTH);

        // Create main button panel
        JPanel buttonPanel = new JPanel(new GridLayout(2, 2, 20, 20));
        buttonPanel.setBorder(BorderFactory.createEmptyBorder(30, 30, 30, 30));
        buttonPanel.setBackground(new Color(240, 240, 240));

        // Create module buttons
        JButton zooButton = createModuleButton("Zoo Management", "icons/zoo.png");
        JButton bankingButton = createModuleButton("Banking System", "icons/banking.png");
        JButton restaurantButton = createModuleButton("Restaurant System", "icons/restaurant.png");
        JButton shapesButton = createModuleButton("Shape Analysis", "icons/shapes.png");

        // Add action listeners to buttons
        zooButton.addActionListener(e -> openZooModule());
        bankingButton.addActionListener(e -> openBankingModule());
        restaurantButton.addActionListener(e -> openRestaurantModule());
        shapesButton.addActionListener(e -> openShapesModule());

        // Add buttons to panel
        buttonPanel.add(zooButton);
        buttonPanel.add(bankingButton);
        buttonPanel.add(restaurantButton);
        buttonPanel.add(shapesButton);

        // Create integration panel
        JPanel integrationPanel = new JPanel(new FlowLayout());
        integrationPanel.setBorder(BorderFactory.createTitledBorder("Cross-Module Integration"));
        JButton zooBankingButton = new JButton("Process Zoo Ticket Sales");
        JButton restaurantBankingButton = new JButton("Process Restaurant Payment");
        JButton shapeVisualButton = new JButton("Visualize Data with Shapes");

        zooBankingButton.addActionListener(e -> processZooTicketSales());
        restaurantBankingButton.addActionListener(e -> processRestaurantPayment());
        shapeVisualButton.addActionListener(e -> visualizeData());

        integrationPanel.add(zooBankingButton);
        integrationPanel.add(restaurantBankingButton);
        integrationPanel.add(shapeVisualButton);

        // Add panels to frame
        frame.add(buttonPanel, BorderLayout.CENTER);
        frame.add(integrationPanel, BorderLayout.SOUTH);

        frame.setVisible(true);
    }

    private JButton createModuleButton(String text, String iconPath) {
        JButton button = new JButton(text);
        button.setFont(new Font("Arial", Font.PLAIN, 18));
        button.setPreferredSize(new Dimension(200, 100));
        button.setVerticalTextPosition(SwingConstants.BOTTOM);
        button.setHorizontalTextPosition(SwingConstants.CENTER);

        // In a real implementation, you would load an actual icon
        // ImageIcon icon = new ImageIcon(iconPath);
        // button.setIcon(icon);

        return button;
    }

    // Module opening methods
    private void openZooModule() {
        JFrame zooFrame = new JFrame("Zoo Management System");
        zooFrame.setSize(600, 400);

        JPanel panel = new JPanel(new BorderLayout());
        JTextArea outputArea = new JTextArea();
        outputArea.setEditable(false);
        JScrollPane scrollPane = new JScrollPane(outputArea);

        // Sample zoo management UI components
        JPanel inputPanel = new JPanel(new GridLayout(4, 2));
        JTextField zooNameField = new JTextField();
        JTextField animalNameField = new JTextField();
        JTextField speciesField = new JTextField();
        JTextField ageField = new JTextField();

        inputPanel.add(new JLabel("Zoo Name:"));
        inputPanel.add(zooNameField);
        inputPanel.add(new JLabel("Animal Name:"));
        inputPanel.add(animalNameField);
        inputPanel.add(new JLabel("Species:"));
        inputPanel.add(speciesField);
        inputPanel.add(new JLabel("Age:"));
        inputPanel.add(ageField);

        JButton addButton = new JButton("Add Animal");
        addButton.addActionListener(e -> {
            // In a real implementation, this would call zooManager.addAnimal()
            outputArea.append("Added animal: " + animalNameField.getText() + "\n");
        });

        JButton listButton = new JButton("List Animals");
        listButton.addActionListener(e -> {
            // In a real implementation, this would call zooManager.listAnimals()
            outputArea.append("Listing animals for zoo: " + zooNameField.getText() + "\n");
        });

        JPanel buttonPanel = new JPanel();
        buttonPanel.add(addButton);
        buttonPanel.add(listButton);

        panel.add(inputPanel, BorderLayout.NORTH);
        panel.add(scrollPane, BorderLayout.CENTER);
        panel.add(buttonPanel, BorderLayout.SOUTH);

        zooFrame.add(panel);
        zooFrame.setVisible(true);
    }

    private void openBankingModule() {
        JFrame bankingFrame = new JFrame("Banking Task Management");
        bankingFrame.setSize(600, 400);

        JPanel panel = new JPanel(new BorderLayout());
        JTextArea outputArea = new JTextArea();
        outputArea.setEditable(false);
        JScrollPane scrollPane = new JScrollPane(outputArea);

        // Sample banking UI components
        JPanel inputPanel = new JPanel(new GridLayout(3, 2));
        JTextField accountField = new JTextField();
        JTextField amountField = new JTextField();

        inputPanel.add(new JLabel("Account Number:"));
        inputPanel.add(accountField);
        inputPanel.add(new JLabel("Amount:"));
        inputPanel.add(amountField);

        JButton depositButton = new JButton("Deposit");
        depositButton.addActionListener(e -> {
            // In a real implementation, this would call bankingService.deposit()
            outputArea.append("Deposited " + amountField.getText() + " to account " + accountField.getText() + "\n");
        });

        JButton withdrawButton = new JButton("Withdraw");
        withdrawButton.addActionListener(e -> {
            // In a real implementation, this would call bankingService.withdraw()
            outputArea.append("Withdrew " + amountField.getText() + " from account " + accountField.getText() + "\n");
        });

        JButton balanceButton = new JButton("Check Balance");
        balanceButton.addActionListener(e -> {
            // In a real implementation, this would call bankingService.getBalance()
            outputArea.append("Balance for account " + accountField.getText() + ": $1000.00\n");
        });

        JPanel buttonPanel = new JPanel();
        buttonPanel.add(depositButton);
        buttonPanel.add(withdrawButton);
        buttonPanel.add(balanceButton);

        panel.add(inputPanel, BorderLayout.NORTH);
        panel.add(scrollPane, BorderLayout.CENTER);
        panel.add(buttonPanel, BorderLayout.SOUTH);

        bankingFrame.add(panel);
        bankingFrame.setVisible(true);
    }

    private void openRestaurantModule() {
        JFrame restaurantFrame = new JFrame("Restaurant Management System");
        restaurantFrame.setSize(600, 400);

        JPanel panel = new JPanel(new BorderLayout());
        JTextArea outputArea = new JTextArea();
        outputArea.setEditable(false);
        JScrollPane scrollPane = new JScrollPane(outputArea);

        // Sample restaurant UI components
        JPanel inputPanel = new JPanel(new GridLayout(3, 2));
        JTextField mealNameField = new JTextField();
        JTextField ingredientField = new JTextField();
        JTextField quantityField = new JTextField();

        inputPanel.add(new JLabel("Meal Name:"));
        inputPanel.add(mealNameField);
        inputPanel.add(new JLabel("Ingredient:"));
        inputPanel.add(ingredientField);
        inputPanel.add(new JLabel("Quantity:"));
        inputPanel.add(quantityField);

        JButton addMealButton = new JButton("Create Meal");
        addMealButton.addActionListener(e -> {
            // In a real implementation, this would call restaurantService.createMeal()
            outputArea.append("Created meal: " + mealNameField.getText() + "\n");
        });

        JButton placeOrderButton = new JButton("Place Order");
        placeOrderButton.addActionListener(e -> {
            // In a real implementation, this would call restaurantService.placeOrder()
            outputArea.append("Placed order for " + quantityField.getText() + " " + mealNameField.getText() + "\n");
        });

        JPanel buttonPanel = new JPanel();
        buttonPanel.add(addMealButton);
        buttonPanel.add(placeOrderButton);

        panel.add(inputPanel, BorderLayout.NORTH);
        panel.add(scrollPane, BorderLayout.CENTER);
        panel.add(buttonPanel, BorderLayout.SOUTH);

        restaurantFrame.add(panel);
        restaurantFrame.setVisible(true);
    }

    private void openShapesModule() {
        JFrame shapesFrame = new JFrame("Shape Parsing & Analysis");
        shapesFrame.setSize(600, 400);

        JPanel panel = new JPanel(new BorderLayout());
        JTextArea outputArea = new JTextArea();
        outputArea.setEditable(false);
        JScrollPane scrollPane = new JScrollPane(outputArea);

        // Sample shapes UI components
        JPanel inputPanel = new JPanel(new FlowLayout());
        JButton parseButton = new JButton("Parse Shapes File");
        JButton countButton = new JButton("Count Shapes");
        JButton areaButton = new JButton("Calculate Total Area");
        JButton displayButton = new JButton("Display Shapes");

        parseButton.addActionListener(e -> {
            // In a real implementation, this would call shapeService.parseFile()
            outputArea.append("Parsing shapes file...\n");
        });

        countButton.addActionListener(e -> {
            // In a real implementation, this would call shapeService.countByType()
            outputArea.append("Counting shapes by type...\n");
        });

        areaButton.addActionListener(e -> {
            // In a real implementation, this would call shapeService.totalArea()
            outputArea.append("Calculating total area...\n");
        });

        displayButton.addActionListener(e -> {
            // In a real implementation, this would call shapeService.displayShapes()
            outputArea.append("Displaying shapes...\n");
        });

        inputPanel.add(parseButton);
        inputPanel.add(countButton);
        inputPanel.add(areaButton);
        inputPanel.add(displayButton);

        panel.add(inputPanel, BorderLayout.NORTH);
        panel.add(scrollPane, BorderLayout.CENTER);

        shapesFrame.add(panel);
        shapesFrame.setVisible(true);
    }

    // Integration methods
    private void processZooTicketSales() {
        // In a real implementation:
        // double ticketSales = zooManager.getTicketSales();
        // bankingService.deposit("zoo1", ticketSales);

        JOptionPane.showMessageDialog(null,
                "Zoo ticket sales processed and deposited to bank account",
                "Integration Success",
                JOptionPane.INFORMATION_MESSAGE);
    }

    private void processRestaurantPayment() {
        // In a real implementation:
        // double billAmount = restaurantService.calculateBill(orderId);
        // bankingService.withdraw(customerId, billAmount);

        JOptionPane.showMessageDialog(null,
                "Restaurant bill processed and payment withdrawn from customer account",
                "Integration Success",
                JOptionPane.INFORMATION_MESSAGE);
    }

    private void visualizeData() {
        JFrame visualFrame = new JFrame("Data Visualization");
        visualFrame.setSize(600, 400);

        // In a real implementation, this would use shapeService to create visualizations
        // of data from other modules (e.g., animal counts as rectangles)

        JPanel panel = new JPanel() {
            @Override
            protected void paintComponent(Graphics g) {
                super.paintComponent(g);
                // Sample visualization - would be dynamic in real implementation
                g.setColor(Color.RED);
                g.fillRect(50, 100, 40, 200); // Sample bar 1
                g.setColor(Color.BLUE);
                g.fillRect(120, 150, 40, 150); // Sample bar 2
                g.setColor(Color.GREEN);
                g.fillRect(190, 50, 40, 250); // Sample bar 3

                g.setColor(Color.BLACK);
                g.drawString("Zoo Animals", 50, 320);
                g.drawString("Bank Accounts", 120, 320);
                g.drawString("Meals Sold", 190, 320);
            }
        };

        visualFrame.add(panel);
        visualFrame.setVisible(true);
    }

    public static void main(String[] args) {
        SwingUtilities.invokeLater(() -> {
            new IMSDashboard();
        });
    }
}